package org.elip.teststatistics.client.ui;

import org.elip.teststatistics.shared.Log;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TabPanelEvent;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout.VBoxLayoutAlign;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;

public class TestLogsPanel extends HorizontalPanel {
	
	private TestStatisticsPanel _testStatisticsPanel;
	private TabPanel _logsTabPanel;
	
	public TestLogsPanel(TestStatisticsPanel testStatisticsPanel) {
		this._testStatisticsPanel = testStatisticsPanel;
	}
	
	
	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		
		int n = Window.getClientHeight() - 2 * 44;

		setWidth("100%");

		_logsTabPanel = new TabPanel();
		_logsTabPanel.setWidth(Window.getClientWidth());
		_logsTabPanel.setPlain(true);
		_logsTabPanel.setResizeTabs(true);
		_logsTabPanel.setMinTabWidth(115);
		_logsTabPanel.setHeight(n);
		_logsTabPanel.setAnimScroll(true);  
		_logsTabPanel.setTabScroll(true);  

		add(_logsTabPanel);
	}
	
	public void removeAllTabs() {
		_logsTabPanel.removeAll();
	}
	
	public void addTab(final Log log, boolean last) {
		
		TabItem item = new TabItem();
		item.setClosable(true);
		if (log.isPassed()) {
			item.setText("<div style='color:green'>" + log.getBuildNumber() + "</div>");
		}
		else { 
			if (log.isFailed()){
				item.setText("<div style='color:red'>" + log.getBuildNumber() + "</div>");
			}
			else {
				item.setText("<div style='color:#FF8C00'>" + log.getBuildNumber() + "</div>");
			}
		}
		
		item.setScrollMode(Scroll.AUTO);
		
		HorizontalPanel buildReportPanel = new HorizontalPanel();
		buildReportPanel.setWidth("100%");
		buildReportPanel.setTableWidth("100%");
		buildReportPanel.setHorizontalAlign(HorizontalAlignment.RIGHT);
		
		String link = null;
		if (log.getProduct().equals("Cloudify")) {
			link = "http://192.168.9.121:8087/sgtest-cloudify/" + log.getBuildNumber() + "/" + log.getGroup() + "/html/index.html";
		}
		else {
			link = "http://192.168.9.121:8087/sgtest-xap/" + log.getBuildNumber() + "/" + log.getGroup() + "/html/index.html";
		}
		Anchor linkToReport = new Anchor("Click here to see full build report", link);
		linkToReport.addStyleName("bold-link");
		linkToReport.setTarget("_blank");
		
		buildReportPanel.add(linkToReport);
		
		HorizontalPanel screenshotsContainer = new HorizontalPanel();
		screenshotsContainer.setWidth("100%");
		screenshotsContainer.setTableWidth("100%");
		screenshotsContainer.setHorizontalAlign(HorizontalAlignment.CENTER);
		screenshotsContainer.addStyleName("background-color-screenshots");
		
		HorizontalPanel screenshotsPanel = new HorizontalPanel();
		screenshotsContainer.setSpacing(5);
		for (String s : log.getLinksToScreenshots()) {
			
			String[] temp = s.split("/");
			String imageName = temp[temp.length - 1];
			
			VerticalPanel vPanel = new VerticalPanel();
			VBoxLayout layout = new VBoxLayout();
			layout.setVBoxLayoutAlign(VBoxLayoutAlign.CENTER);
			vPanel.setLayout(layout);
			vPanel.setStyleAttribute("margin-left", "15px");
			
			Image img = new Image(s);
			img.setWidth("300px");
			img.setHeight("200px");
			//vPanel.add(img);
			
			HorizontalPanel linkPanel = new HorizontalPanel();
			linkPanel.setWidth("100%");
			linkPanel.setTableWidth("100%");
			linkPanel.setHorizontalAlign(HorizontalAlignment.CENTER);
			
			
			Anchor imageLink = new Anchor("", s);
			imageLink.getElement().appendChild(img.getElement());
			imageLink.setTarget("_blank");
			vPanel.add(imageLink);
			
			Anchor linkToImage = new Anchor(imageName, s);
			linkToImage.setTarget("_blank");
			linkPanel.add(linkToImage);
			
			vPanel.add(linkPanel);
			
			screenshotsPanel.add(vPanel);
		}
		
		screenshotsContainer.add(screenshotsPanel);
		
		item.add(buildReportPanel);
		item.add(new HTML("<br>"));
		if (log.getLinksToScreenshots().length != 0) {
			item.add(screenshotsContainer);
			item.add(new HTML("<br>"));
		}
		item.addText(log.getLog());
		item.addListener(Events.Close, new Listener<TabPanelEvent>() {
			@Override
			public void handleEvent(TabPanelEvent evt) {
				
				if (log.isFailed()) {
					int current = _testStatisticsPanel.getFailed();
					_testStatisticsPanel.setFailed((current - 1 ));
				}
				if (log.isPassed()) {
					int current = _testStatisticsPanel.getPassed();
					_testStatisticsPanel.setPassed((current - 1 ));
				}
				if (log.isSkipped()) {
					int current = _testStatisticsPanel.getSkipped();
					_testStatisticsPanel.setSkipped((current - 1 ));
				}
			}
		});
		_logsTabPanel.add(item);
		if (last) _logsTabPanel.setSelection(item);
		
	}


	public void setTabPanelWidth(int clientWidth) {
		_logsTabPanel.setWidth(clientWidth);
		
	}
}
